package com.zhang.crm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.zhang.crm.mapper.TCluemapper;
import com.zhang.crm.mapper.TCustomermapper;
import com.zhang.crm.pojo.TClue;
import com.zhang.crm.pojo.TCustomer;
import com.zhang.crm.pojo.res.CustomerReq;
import com.zhang.crm.service.CustomerService;
import com.zhang.crm.util.UserUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private TCustomermapper tCustomermapper;
    @Resource
    private TCluemapper tCluemapper;
    @Override
    public int addCustomer(TCustomer tCustomer) {
        tCustomer.setCreateTime(new Date());
        tCustomer.setCreateBy(UserUtil.getUserInfo().getId());
        int insert = tCustomermapper.insert(tCustomer);
        return insert;
    }

    @Override
    public void downLoadCustomerExcel(HttpServletResponse res) {
        //获取客户信息
        List<CustomerReq> customerReqs = tCluemapper.batchCustomerReqList();

        try {
            EasyExcel.write(res.getOutputStream(), CustomerReq.class).sheet("模板").doWrite(customerReqs);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void chooseDownLoadCustomerExcel(HttpServletResponse res,List<Integer> ids) {
        //获取客户信息
        List<CustomerReq> customerReqs = tCluemapper.selectCustomerReqList(ids);
        try {
            EasyExcel.write(res.getOutputStream(), CustomerReq.class).sheet("模板").doWrite(customerReqs);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
